package com.youdelu.road.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youdelu.road.R;
import com.youdelu.road.bean.BgAttr;
import com.youdelu.road.bean.FileDir;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

public class ListAdapter extends BaseAdapter{
    private LayoutInflater inflater;
    private OnClickInterface onClickInterface;
    private Context context=null;
    private List<BgAttr> dirs = null;
    private int bg_id ;

    private class Holder{
        TextView item_tex;

        public Button getItme_open() {
            return itme_open;
        }

        public void setItme_open(Button itme_open) {
            this.itme_open = itme_open;
        }

        public Button getItem_del() {
            return item_del;
        }

        public void setItem_del(Button item_del) {
            this.item_del = item_del;
        }

        Button itme_open ;
        Button item_del ;
        public TextView getItem_tex() {
            return item_tex;
        }
        public void setItem_tex(TextView item_tex) {
            this.item_tex = item_tex;
        }
    }
    //构造方法
    public ListAdapter(Context context, OnClickInterface onClickInterface,List<BgAttr> dirs) {
        this.inflater= LayoutInflater.from(context);
        this.context = context;
        this.onClickInterface = onClickInterface;
        if(dirs==null){
            dirs = new ArrayList<>();
        }
        BgAttr bgAttr = new BgAttr();
        bgAttr.setId(0);
        bgAttr.setPath("file:///android_asset/bg.png");
        bgAttr.setName("默认");
        dirs.add(bgAttr);
        this.dirs =  dirs;
    }


    public void update(List<BgAttr> dirs,int bgid){
        if(dirs==null){
            dirs = new ArrayList<>();
        }
        BgAttr bgAttr = new BgAttr();
        bgAttr.setId(0);
        bgAttr.setPath("file:///android_asset/bg.png");
        bgAttr.setName("默认");
        dirs.add(bgAttr);
        this.dirs =  dirs;
        this.bg_id = bgid ;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return dirs.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        Holder holder;
        if(view==null){
            view= inflater.inflate(R.layout.view_listview_item,null);
            holder=new Holder();
            holder.item_tex= view.findViewById(R.id.item_text);
            holder.itme_open = view.findViewById(R.id.open);
            holder.item_del = view.findViewById(R.id.del);
            view.setTag(R.id.tag,holder);
        }else{
            holder=(Holder) view.getTag(R.id.tag);
        }

        view.setTag(R.id.tag_first,dirs.get(position));
        String  n = "";
        if(dirs.get(position).getId()==bg_id){
            n = " -> ";
        }
        holder.item_tex.setText(n+dirs.get(position).getName());
        holder.item_del.setTag(R.id.tag_first,dirs.get(position));
        if(dirs.get(position).getId()==0){
            holder.item_del.setText("还原");
        }else{
            holder.item_del.setText("删除");
        }
        holder.item_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickInterface.del((BgAttr)v.getTag(R.id.tag_first));
            }
        });
        holder.itme_open.setTag(R.id.tag_first,dirs.get(position));
        holder.itme_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BgAttr attr = (BgAttr)v.getTag(R.id.tag_first);
                onClickInterface.open(attr);
            }
        });

        return view;
    }
    public interface OnClickInterface {
        public void open(BgAttr attr);
        public void del(BgAttr attr);
    }
}